package com.ruyuan2020.im.code;

public class ContainerWithMostWater {


        public int maxArea(int[] height) {
            int left = 0;
            int right = height.length - 1;
            int maxWater = 0;

            while (left < right) {
                // 计算当前面积
                int currentWater = (right - left) * Math.min(height[left], height[right]);
                maxWater = Math.max(maxWater, currentWater);

                // 移动较小高度的指针
                if (height[left] < height[right]) {
                    left++;
                } else {
                    right--;
                }
            }

            return maxWater;
        }

        public static void main(String[] args) {
            ContainerWithMostWater solution = new ContainerWithMostWater();
            int[] height = {1,8,6,2,5,4,8,3,7};
            int maxWater = solution.maxArea(height);
            System.out.println("The maximum amount of water the container can store is: " + maxWater);
        }
    }


